#!/bin/sh

#sd_booted() {
#  [[ -d run/systemd/system && ! -L run/systemd/system ]]
#}
 
add_privs() {
  if ! setcap "$2" "$1" 2>/dev/null; then
    echo "==> Warning: setcap failed, falling back to setuid root on /$1"
    chmod u+s "$1"
  fi
}

post_common() {
  systemd-machine-id-setup

  udevadm hwdb --update
  journalctl --update-catalog

  #if sd_booted; then
  #  systemctl --system daemon-reexec
  #fi
}

_208_changes() {
  chown root:systemd-journal var/log/journal
  chmod 2755 var/log/journal

  if [ -e var/lib/backlight ] && [ ! -e var/lib/systemd/backlight ]; then
    mv -T var/lib/backlight var/lib/systemd/backlight
  fi

  if [ -e var/lib/random-seed ] && [ ! -e var/lib/systemd/random-seed ]; then
    mv -T var/lib/random-seed var/lib/systemd/random-seed
  fi
}

post_install() {
  post_common 

  # systemd-boot install
  if [ -d sys/firmware/efi/efivars ]; then
    /usr/bin/bootctl update
    echo ":: Run '/usr/bin/bootctl install' to make systemd-boot your default bootloader"
  fi
  mkdir -p /boot/efi/systemd-boot
}

post_upgrade() {
  post_common
  
  #if sd_booted; then
  #  systemctl restart systemd-logind.service
  #fi
  
  if [ "$(vercmp 208-1 "$2")" -eq 1 ]; then
    _208_changes
  fi
  
  if [ -d sys/firmware/efi/efivars ]; then
    echo ":: Updating bootctl"
    /usr/bin/bootctl update
  
    if [ "$(vercmp 1:220-1 "$2")" -eq 1 ]; then
      echo ":: Installing systemd-boot"
      mkdir -p /boot/efi/systemd-boot
      /usr/bin/bootctl install
    fi
  fi
}

